package controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import po.LowAudit;
import po.MerchantAudit;
import service.LowAuditManager;
import service.MerchantAuditManager;

@Controller
@RequestMapping("/merchant")
public class MerchantController {
	
	@Autowired
	private MerchantAuditManager mam;
	@Autowired
	private LowAuditManager lam;

	@RequestMapping("/list")
	public String listMerchantAudit(Model model){
		model.addAttribute("merchantList", mam.listMerchantAudit());
		return "audit_merchant";
	}
	
	@RequestMapping("/audit/{maId}/{status}")
	@ResponseBody
	public String changeMerchantStatus(@PathVariable String maId, @PathVariable int status){
		String sign = mam.auditMerchant(maId, status);
		if(sign != null && !sign.equals("0")){
			return "{\"msg\" : \"SUCCESS\"}";
		} else {
			return "{\"error\" : \"operation failure\"}";
		}
	}
	
	@RequestMapping("/lowGrade")
	public String listLowMerchant(Model model){
		model.addAttribute("merchantList", lam.listAllLowAudit());
		return "low_grade_merchant";
	}
	
	@RequestMapping("/lowGrade/audit/{mId}/{status}")
	@ResponseBody
	public String auditLow(@PathVariable String mId, @PathVariable int status){
		String sign = lam.updateLowAuditStatus(mId, status);
		if(!sign.equals("0") && sign != null){
			return "{\"msg\" : \"SUCCESS\"}";
		} else {
			return "{\"error\" : \"operation failure\"}";
		}
	}
	
	@RequestMapping("/listBlackMerchant")
	@ResponseBody
	public List<String> listBlackMerchant(){
		List<String> list = mam.listBlackMerchant();
		return list;
	}
	
//	@RequestMapping
	
	@RequestMapping("/checkStatus/{mId}")
	@ResponseBody
	public Integer ckeckMerchantStatus(@PathVariable String mId){
		return mam.checkMerchantStatus(mId);
	}
}
